package jmine.tec.web.wicket.component.menu.cfg;

import java.util.List;

/**
 * Renames menu items
 * 
 * @author lundberg
 */
public class MenuConfigRenamer {

    private final MenuConfig menuConfig;

    private final List<String> path;

    /**
     * Constructor
     * 
     * @param menuConfig menuConfig
     * @param path path
     */
    public MenuConfigRenamer(MenuConfig menuConfig, List<String> path) {
        this.menuConfig = menuConfig;
        this.path = path;
    }

    /**
     * Given the new label, replaces the old one. This operation might trigger a merge if there is a collision.
     * 
     * @param newLabel newLabel
     * @return MenuConfigItem
     */
    public MenuConfigItem to(String newLabel) {
        MenuConfigItem renamed = this.menuConfig.remove(this.path);
        renamed.setLabel(newLabel);
        return this.menuConfig.addItem(renamed, this.path.subList(0, this.path.size() - 1));
    }
}
